/*

Summary:
An object wrapper for critical section.

Remark:
None.

Version:
0.0.2

LastUpdatedDate;
2012-09-21

CreatedDate:
2012-09-05

*/

#pragma once

#include <Windows.h>


namespace Zaf {
namespace Sync {



class CriticalSection {

public:
	CriticalSection();
	explicit CriticalSection(DWORD spinCount);
	~CriticalSection();

	void SetSpinCount(DWORD spinCount);

	void Enter();
	bool TryEnter();

	void Leave();

private:
	CriticalSection(const CriticalSection&);
	const CriticalSection& operator=(const CriticalSection&);

	CRITICAL_SECTION m_cs;
};



inline CriticalSection::CriticalSection() {
	::InitializeCriticalSection(&m_cs);
}

inline CriticalSection::CriticalSection(DWORD spinCount) {
	::InitializeCriticalSectionAndSpinCount(&m_cs, spinCount);
}

inline CriticalSection::~CriticalSection() {
	::DeleteCriticalSection(&m_cs);
}

inline void CriticalSection::SetSpinCount(DWORD spinCount) {
	::SetCriticalSectionSpinCount(&m_cs, spinCount);
}

inline void CriticalSection::Enter() {
	::EnterCriticalSection(&m_cs);
}

inline bool CriticalSection::TryEnter() {
	::TryEnterCriticalSection(&m_cs);
}

inline void CriticalSection::Leave() {
	::LeaveCriticalSection(&m_cs);
}


}
}